<?php
include_once("../modules/checkA.php");
include_once("../../config.php");
$plate=select_one_line_all_items($table_Prefix."plate","mid = 'inventory'");
?>


<?
//用来判断当前系统属于什么板块
$system_part = $plate['mid'];
include_once("detail-list-function.php");
?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include_once(ROOT_PATH_.'/controlbase/modules/head.php'); ?>
</head>

<body>





<?php


$new_detail_number        = addslashes($_REQUEST['number']);
$new_detail_number        = abs($new_detail_number);

$new_detail_people        = addslashes($_REQUEST['people']);
$new_detail_remark        = addslashes($_REQUEST['remark']);
$new_detail_applydate     = addslashes($_REQUEST['applydate']);
$new_detail_materialnid   = addslashes($_REQUEST['materialnid']);
$new_detail_departmentnid = addslashes($_REQUEST['department']);
$new_detail_price         = addslashes($_REQUEST['price']);




$material=select_one_line_all_items($table_Prefix."material","nid='".$new_detail_materialnid."'");


//先检查一下库存状态
$material_sum=sum_total($table_Prefix."inventory","number"," materialnid = '".$new_detail_materialnid."'");



if(
    ($new_detail_number<0)&&
    (abs($new_detail_number)>$material_sum)
  )
{
?>
<script type="text/javascript">
    alert("超出库存，请重新领取.");
   document.location.href = "detail-add.php?nav-itemed=inventory";
</script>

<?

}

else{




//设置需要搜索的表格的前缀后缀
$table_Suffix = "inventory";
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀



$sql = "INSERT INTO $table (
number,
people,
remark,
applydate,
materialnid,
departmentnid,
price
)

value (
'$new_detail_number',
'$new_detail_people',
'$new_detail_remark',
'$new_detail_applydate',
'$new_detail_materialnid',
'$new_detail_departmentnid',
'$new_detail_price'
)";



$query = mysql_query($sql) or die ('添加数据失败！！');




  //插入数据之后，还要判断一下，增减库存之后，物料的库存是否告急

    //计算领取的平均数
    $intentory_avg=abs(count_avg($table_Prefix."inventory","number"," materialnid = '".$new_detail_materialnid."' and number < 0"));

    $material_sum=sum_total($table_Prefix."inventory","number"," materialnid = '".$new_detail_materialnid."'");



    if($intentory_avg>$material_sum*2){
        $material_status        = "紧张";
    }
    else
    {
        $material_status        = "充足";
    }






//设置需要搜索的表格的前缀后缀
    $table_Suffix = "material";
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀




$sql = "update $table SET
stockstatus= '$material_status'
where nid=$new_detail_materialnid";


$query = mysql_query($sql) or die ('添加数据失败！！');









//后来决定，财务数据都从这些表中直接读取吧。
////这里应该要根据数目加减来到和财务系统联动。
//
//
//
//
//
//
//
//
////设置需要搜索的表格的前缀后缀
//    $table_Suffix = "finance";
//    $table = $table_Prefix . $table_Suffix;
////设置需要搜索的表格的前缀后缀
//
//
//
//
//
//    $new_applydate    = $new_detail_applydate;
//    $new_classify     = "采购";
//    $ip               = $_SERVER["REMOTE_ADDR"];
//    $new_authur       = $_SESSION['nid'];
//    $new_remark       = "进货系统生成信息";
//    $new_status       = "生效";
//    $masterid         = $master['nid'];
//    $new_number       = $master['nid'];
//    $new_name         = "购进".$material['name'];
//    $new_cost         =$new_detail_price*$new_detail_number;
//
//
//
//$sql_finance = "INSERT INTO $table (
//name,
//departmentid,
//cost,
//status,
//applydate,
//classify,
//authur,
//ip,
//masterid,
//remark
//)
//
//value (
//'$new_name',
//'$new_detail_departmentnid',
//'$new_cost',
//'$new_status',
//'$new_applydate',
//'$new_classify',
//'$new_authur',
//'$ip',
//'$masterid',
//'$new_remark'
//)";
//
//
//
//    $query = mysql_query($sql_finance) or die ('添加数据失败！！');
//
//
//



    mysql_close($con);

}
?>





<script type="text/javascript">
   alert("输入信息已保存，点击确认返回继续操作.");
   document.location.href = "detail-list.php?nav-itemed=inventory";
</script>
</body>